
import network
import time

import modular_dht11
from mqtt_client import link_mqtt
#from model_temperature import getTemp


# wifi


def do_connect():
    wifi_list = [
        {
            "ssid": "PDCN",
            "password": "1234567890"
        },
        {
            "ssid": "ANRUI",
            "password": "89498841"
        }
    ]
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        ssids = wlan.scan()
        for ssid in ssids:
            for wifi in wifi_list:
                if ssid[0] == wifi.get("ssid"):
                    wlan.connect(wifi.get("ssid"), wifi.get("password"))
                    print('connecting to network...')
                    break
        # wlan.connect(SSID, PASSWORD)

    start = time.time()
    while not wlan.isconnected():
        time.sleep(1)
        if time.time() - start > 5:
            print("connect timeout!")
            break

    if wlan.isconnected():
        print('network config:', wlan.ifconfig())
        infinite_loop()


# 无限循环 调用查询温湿度
def infinite_loop():
    while 1 == 1:
        print("再来一次")
        modular_dht11.get_current_context()
        time.sleep(3)
